package com.bnz.portal.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;

import com.bnz.common.utils.Query;
import com.bnz.portal.dao.ContentDao;
import com.bnz.portal.entity.ContentEntity;
import com.bnz.portal.service.ContentService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("contentService")
public class ContentServiceImpl extends ServiceImpl<ContentDao, ContentEntity> implements ContentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContentEntity> page = this.page(
                new Query<ContentEntity>().getPage(params),
                new QueryWrapper<ContentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    // @Cacheable 注解关键点: 先查缓存中的,如果没有再查数据库,查数据库会调用方法,不查不调用
    // unless 除非返回的集合中没有数据,否则必须放到缓存中
    @Cacheable(value = "contents" ,key = "#root.methodName", unless = "#result.size() == 0")
    public List<ContentEntity> findAll() {
        System.out.println("缓存中没有数据,正在使用数据库进行查询.......");
        return this.list();
    }

}